#!/bin/bash

#
# configure iptables, rsh, rcp on remote server 
#

. $SFROOT/lib/Echo.sh
. $SFROOT/testcases/Network_Service/lib/Net.sh

function setup_env()
{

    RHOST=${RHOST}
    RUSERNAME=${RUSERNAME}
    PASSWORD=${PASSWORD}

if [ -z ${RHOST} -o -z ${RUSERNAME} -o -z ${PASSWORD} ];then
    echo "get value from config.xml failed, setup env failed."
    exit 1 
fi

# user must be root
    [ "x$(id -ru)" != "x0" ] && { echo "user must be root."; exit 1; }

# check rhost is reachable
    ping -c 1 ${RHOST} | grep -i "Destination Host Unreachable"
    [ $? -eq 0 ] && { echo "${RHOST} is unreachable, test end."; exit 1; }

# stop iptables on local

    status_daemon iptables
    RC=$?
    if [ $RC -eq 0 ]; then
        stop_daemon iptables
        [ $? -ne 0 ] && { echo "local stop iptables failed, test end."; exit 1; }
    fi

    # clear iptable rules
    iptables -F


# stop iptables on remote server
    SSHRemote ${RHOST} ${PASSWORD} "command -v systemctl && /bin/systemctl stop iptables.service || service iptables stop; iptables -F"

    EchoResult "command \"spawn ssh -n $RHOST \"iptables stop\"\""

# config rsh
#### check xinetd and rsh
   local RESULT=`SSHRemote ${RHOST} ${PASSWORD} "rpm -qa | grep rsh-server"`
   echo "${RESULT}" | grep rsh-server
   EchoResult "rsh-server installed"

   RESULT=`SSHRemote ${RHOST} ${PASSWORD} "rpm -qa | grep xinetd"`
   echo "${RESULT}" | grep xinetd
   EchoResult "xinetd installed"

#### add rhost to local /root/.rhosts
   
   RHOSTS_CONF="/root/.rhosts"
   
   if [ ! -s "${RHOSTS_CONF}" ]; then
       echo "${RHOST} root" > ${RHOSTS_CONF}
   else
       grep "${RHOST}" "${RHOSTS_CONF}" || echo "${RHOST} root" >> ${RHOSTS_CONF}      
   fi

#### add local_ip addr to remote server
    if [ $RHOST = `hostname` -o $RHOST = "localhost" ]; then
        echo "test on locahost"
    else
        LOCAL_IP=`ifconfig | grep 'inet ' | grep -m 1 -v '127.0.0.1'  | awk '{print $2}' | tr -d "addr:"`
        export LOCAL_IP=${LOCAL_IP}

        SSHRemote ${RHOST} ${PASSWORD} "echo $LOCAL_IP root > $RHOSTS_CONF"

        EchoResult "command \"spawn ssh -n $RHOST \"echo $LOCAL_IP root > $RHOSTS_CONF\"\""
    fi
    

#### config /etc/xinetd.d/rsh on remote server

    RSH_CONF="/etc/xinetd.d/rsh"
    SSHRemote ${RHOST} ${PASSWORD} "test -e ${RSH_CONF}  && mv -f ${RSH_CONF} ${RSH_CONF}.bak"
    TMP_RSH_CONF="/tmp/rshtest"
    cat > ${TMP_RSH_CONF} <<-EOF
service shell
{
	socket_type		= stream
	wait			= no
	user			= root
	log_on_success		+= USERID
	log_on_failure 		+= USERID
	server			= /usr/sbin/in.rshd
	disable			= no
}
EOF

    SCPRemote $RHOST $PASSWORD ${TMP_RSH_CONF} $RSH_CONF

    EchoResult "command \"spawn scp ${TMP_RSH_CONF} root@$RHOST:$RSH_CONF\""
    rm -f $TMP_RSH_CONF


#### add rsh,rcp to /etc/securetty on remote server

    SECURETTY="/etc/securetty" 

    SSHRemote $RHOST $PASSWORD "grep rsh $SECURETTY || echo rsh >> $SECURETTY; \
                              grep rcp $SECURETTY || echo rcp >> $SECURETTY"


    EchoResult "command \"spawn ssh -n $RHOST \"grep rsh $SECURETTY || echo rsh >> $SECURETTY; \
                                                grep rcp $SECURETTY || echo rcp >> $SECURETTY\"\""


#### restart xinetd service on remote server

    SSHRemote $RHOST $PASSWORD "command -v systemctl && /bin/systemctl restart xinetd.service || service xinetd restart"


    EchoResult "command \"spawn ssh -n $RHOST \"xinetd restart\"\""

#### test rsh

    expect -c "
        spawn ${RSH} -n $RHOST whoami
            expect {
                \"Connection refused\" { send_user \"rsh not started on $RHOST\n\"; exit 1; }
                \"Permission denied\" { send_user \"pls check .rhosts\n\"; exit 1; }
                \"Host address mismatch\" { send_user \"rsh failed, pls check\n\"; exit 1; }
                \"root\" { send_user \"configure rsh on $RHOST success\n\";  }
            }
        expect eof
    "
    EchoResult "spawn ${RSH} -n $RHOST whoami"

#### rcp cmd.sh to remote
#    CMD_FILE=/tmp/cmd.sh 
#    export CMD_FILE
#    ${RCP} ${SFROOT}/lib/cmd.sh root@$RHOST:/tmp/
#    EchoResult "rcp cmd.sh to remote"

#### echo source to .bash_profile
    #PROFILE=/etc/profile
    #${RSH} -l root -n $RHOST "grep \"source /tmp/cmd.sh\" ${PROFILE} || echo \"source /tmp/cmd.sh\" >> ${PROFILE}"

return 0

}
